package  
{
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.FlxTileblock;
	import org.flixel.FlxU;
	
	/**
	 * ...
	 * @author maliu
	 */
	public class GameState extends FlxState 
	{
		[Embed(source = "../res/tech_tiles.png")]
		protected var TitleImgRef:Class;
		
		protected var p_player:Player;
		protected var p_groundTile:FlxTileblock;
		
		private var m_bulletList:FlxGroup;
		private var m_platformList:FlxGroup;
		private var m_enemyList:FlxGroup;
		
		public static const MAP_WIDTH:int = 640;
		public static const MAP_HEIGHT:int = 480;
		public static const TITLE_BLOCK_WIDTH:int = 8;
		public static const MAX_TITLE_BLOCK_NUM:int = 4;
		public static const MIN_TITLE_BLOCK_NUM:int = 2;
		public static const RANDOM_LOOP_COUNT:int = 5;
		public static const ENEMY_COUNT:int = 10;
		
		public function GameState() 
		{
			
		}
		override public function create():void
		{
			//this.add(new FlxText(50, 100, 200, "Hello Flixel!"));
			//this.addGround();
			this.addPlayer();
			this.addbullets();
			this.addStateFunction();
			this.addRandomMap();
			this.addEnemies();
		}
		override public function update():void
		{
			//添加碰撞，角色与地板的碰撞
			//FlxU.collide(p_player, p_groundTile);
			//FlxU.collide(m_bulletList, p_groundTile);
			FlxU.collide(p_player, m_platformList);
			FlxU.collide(m_bulletList, m_platformList);
			FlxU.collide(m_enemyList, m_platformList);
			FlxU.overlap(m_bulletList, m_enemyList, hitEnemy);
			//p_player.collide(p_groundTile);
			super.update();
		}
		private function addGround():void
		{
			p_groundTile = new FlxTileblock(0, 200, 640, 20);
			p_groundTile.loadTiles(TitleImgRef, 0, 0, 0);
			this.add(p_groundTile);
		}
		private function addPlayer():void
		{
			p_player = new Player(100, 180);
			this.add(p_player);
		}
		private function addStateFunction():void
		{
			//视角跟随角色
			FlxG.follow(p_player, 10);
			FlxG.followAdjust(0.0, 0.0);
			//设定视角跟随范围
			FlxG.followBounds(0, 0, 640, 480);
			//设定碰撞检测范围
			FlxU.setWorldBounds(0, 0, 640, 480);
		}
		private function addbullets():void
		{
			m_bulletList = new FlxGroup();
			var t_bullet:Bullet;
			for (var i:int = 0; i < 10; i++ )
			{
				t_bullet = new Bullet();
				m_bulletList.add(t_bullet);
			}
			this.add(m_bulletList);
			p_player.bulletList = m_bulletList;
		}
		private function addRandomMap():void
		{
			m_platformList = new FlxGroup();
			var t_tile:FlxTileblock;
			//
			t_tile = new FlxTileblock(0, 0, 8, MAP_HEIGHT);
			t_tile.loadTiles(TitleImgRef);
			m_platformList.add(t_tile);
			//
			t_tile = new FlxTileblock(0, MAP_HEIGHT - 8, MAP_WIDTH, 8);
			t_tile.loadTiles(TitleImgRef);
			m_platformList.add(t_tile);
			//
			t_tile = new FlxTileblock(MAP_WIDTH - 8, 0, 8, MAP_HEIGHT);
			t_tile.loadTiles(TitleImgRef);
			m_platformList.add(t_tile);
			//
			t_tile = new FlxTileblock(0, 0, MAP_WIDTH, 8);
			t_tile.loadTiles(TitleImgRef);
			m_platformList.add(t_tile);
			
			this.add(m_platformList);
			
			for (var i:int = 0; i < 150; i++ )
			{
				this.addRandomTile();
			}
		}
		/**
		 * 生成随机tile
		 */
		private function addRandomTile():void
		{
			var t_tile:FlxTileblock;
			var t_blockWidth:int;
			var t_blockHeight:int;
			var t_blockX:int;
			var t_blockY:int;
			var t_isOverlap:Boolean = false;
			var t_loopCount:int = 0;
			
			do
			{
				t_blockWidth = int(Math.random() * (MAX_TITLE_BLOCK_NUM - MIN_TITLE_BLOCK_NUM) + MIN_TITLE_BLOCK_NUM) * TITLE_BLOCK_WIDTH;
				t_blockHeight = int(Math.random() * (MAX_TITLE_BLOCK_NUM - MIN_TITLE_BLOCK_NUM) + MIN_TITLE_BLOCK_NUM * TITLE_BLOCK_WIDTH);
				t_blockX = int(Math.random() * MAP_WIDTH / TITLE_BLOCK_WIDTH - MAX_TITLE_BLOCK_NUM) * TITLE_BLOCK_WIDTH;
				t_blockY = int(Math.random() * MAP_HEIGHT / TITLE_BLOCK_WIDTH - MAX_TITLE_BLOCK_NUM) * TITLE_BLOCK_WIDTH;
				t_tile = new FlxTileblock(t_blockX, t_blockY, t_blockWidth, t_blockHeight);
				t_tile.loadTiles(TitleImgRef);
				
				for each(var t_existTile:FlxTileblock in m_platformList.members)
				{
					t_isOverlap = t_tile.overlaps(t_existTile);
					if (t_isOverlap)
					{
						break;
					}
				}
				t_loopCount++;
			}while (t_isOverlap && t_loopCount < RANDOM_LOOP_COUNT);
			m_platformList.add(t_tile);
		}
		private function addEnemies():void
		{
			m_enemyList = new FlxGroup();
			var t_enemyCount:int = 0;
			var t_enemy:Enemy;
			
			for each(var t_tile1:FlxTileblock in m_platformList.members)
			{
				var t_enemyStartX:Number = t_tile1.x;
				var t_enemyStartY:Number = t_tile1.y;
				var t_collides:Boolean = false;
				for each(var t_tile2:FlxTileblock in m_platformList.members)
				{
					var t_collideX:Number = t_enemyStartX;
					var t_collideX2:Number = t_collideX + TITLE_BLOCK_WIDTH;
					var t_collideY:Number = t_enemyStartY - TITLE_BLOCK_WIDTH;
					var t_collideY2:Number = t_enemyStartY;
					if (t_tile1 != t_tile2 && 
						(t_tile2.overlapsPoint(t_collideX, t_collideY)
						|| t_tile2.overlapsPoint(t_collideX, t_collideY2)
						|| t_tile2.overlapsPoint(t_collideX2, t_collideY)
						|| t_tile2.overlapsPoint(t_collideX2, t_collideY2)))
					{
						t_collides = true;
						break;
					}
				}
				if (!t_collides)
				{
					t_enemy = new Enemy(t_enemyStartX, t_enemyStartY, t_tile1.width);
					m_enemyList.add(t_enemy);
					t_enemyCount++;
				}
				if (t_enemyCount == ENEMY_COUNT)
				{
					break;
				}
			}
			this.add(m_enemyList);
		}
		private function hitEnemy(_obj1:FlxObject, _obj2:FlxObject):void
		{
			if (_obj1.dead)
			{
				return;
			}
			_obj1.hurt(0);
			_obj2.hurt(1);
		}
	}

}